import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  // 项目名
  projectName: Cookies.get('app.project'),
  // API接口
  baseUrl: Cookies.get('app.baseUrl'),
  // 分页数
  pageSize: Cookies.get('app.pageSize') || 10
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },

  // 设置项目名称
  SET_PROJECT: (state, projectName) => {
    console.log('88525555', process.env)
    state.projectName = projectName
    Cookies.set('app.project', state.projectName)
  },

  // 设置基本请求URL
  SET_BASE_URL: (state, projectName) => {
    console.log(projectName.toUpperCase(), '8888')
    console.log(process.env['VUE_APP_' + projectName.toUpperCase()], '888668')
    state.baseUrl = process.env['VUE_APP_' + projectName.toUpperCase()]
    Cookies.set('app.baseUrl', state.baseUrl)
  },

  // 设置分页数
  SET_PAGE_SIZE: (state, pageSize) => {
    state.pageSize = pageSize
    Cookies.set('app.pageSize', pageSize)
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  setProject({ commit }, projectName) {
    commit('SET_PROJECT', projectName)
  },
  setBaseUrl({ commit }, projectName) {
    commit('SET_BASE_URL', projectName)
  },
  setPageSize({ commit }, pageSize) {
    commit('SET_PAGE_SIZE', pageSize)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
